using System;
using System.Collections.Generic;
using UnityEngine;

namespace UIFramework.Panel
{
    /// <summary>
    /// 规定界面位于哪个层，便于管理
    /// </summary>
    public enum PanelPriority
    {
        None = 0, // 默认层
        Prioritary = 1, // 优先层
        Tutorial = 2, // 引导层
        Blocker = 3 // 阻塞层
    }

    [Serializable]
    public class PanelPriorityLayerListEntry
    {
        [SerializeField]
        [Tooltip("指定下面板的优先级")]
        private PanelPriority priority;
        [SerializeField]
        [Tooltip("此优先级下的父节点")]
        private Transform targetParent;
        
        public Transform TargetParent
        {
            get { return targetParent; }
            set { targetParent = value; }
        }

        public PanelPriority Priority
        {
            get { return priority; }
            set { priority = value; }
        }

        public PanelPriorityLayerListEntry(PanelPriority prio, Transform parent)
        {
            priority = prio;
            targetParent = parent;
        }
    }

    [Serializable]
    public class PanelPriorityLayerList
    {
        [SerializeField] 
        [Tooltip("根据面板的优先级查找并存储对应的GameObject。渲染优先级由这些GameObject在层级结构中的顺序决定")]
        private List<PanelPriorityLayerListEntry> paraLayers = null;

        private Dictionary<PanelPriority, Transform> lookup;

        public Dictionary<PanelPriority, Transform> ParaLayerLookup 
        {
            get 
            {
                if (lookup == null || lookup.Count == 0) 
                {
                    CacheLookup();
                }

                return lookup;
            }
        }

        private void CacheLookup()
        {
            lookup = new Dictionary<PanelPriority, Transform>();
            for (int i = 0; i < paraLayers.Count; i++)
            {
                lookup.Add(paraLayers[i].Priority, paraLayers[i].TargetParent);
            }
        }

        public PanelPriorityLayerList(List<PanelPriorityLayerListEntry> entries)
        {
            paraLayers = entries;
        }
    }
}